iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0

Day 21 基於事件消息傳遞服務拓展實戰 - KEDA 組建測試之路 RabbitMQ 篇

本日重點與方向 (TAG): GCP、Google Cloud Platform、Pub/Sub、Publisher、Subscriber、Tranciver、Reciver、Topic、KEDA、Event Trigger、RabbitMQ、Comsumer
在之前的 GCP 提供的 Pub/Sub 中,我們有提到說事件觸發機制進行拓展的一個機制,後來有看到有人提 KEDA 的這個事件觸發控制器,今年的 COSCUP 也有開源貢獻的工程師來演講,有介紹一些事件觸發的概念跟作法,詳細的就去看一下 COSCUP 的議程後續的內容詳述,我們這邊就直接來時實戰一下,這邊預計 KEDA 會弄個兩天做一下測試,我們今天就先去搞一下官方提供的 RabbitMQ 的測試吧,明天會再基於 GCP 的 Pub/Sub 做一個事件觸發的測試。

本次使用設備資訊

Network Switch

  • 數量: 1
  • 型號: D-Link 1210-28 (L2 Switch)

Bare Metal

Master Node

  • 數量: 1
  • Ubuntu: 16.04 / 18.04
  • Docker Version: 19.03
  • CPU: E3-1230_V3 ^ 1
  • RAM: 16GB
  • OS_Disk: 120 GB (SSD)
  • Network: 1Gbps

Worker Node

  • 數量: 2
  • Ubuntu: 16.04 / 18.04
  • Docker Version: 19.03
  • CPU: E3-1230_v3
  • RAM: 8 GB
  • OS_Disk: 120 GB (SSD)
  • Network: 1Gbps

KEDA Event Trigger 安裝

https://github.com/kedacore/keda
https://keda.sh/

Helm 部署方案

  • Helm 安裝

    • Linux
      sudo snap install helm
      
    • MacOS
      brew install helm
      
  • KEDA 安裝

    1. 添加 helm repository
      helm repo add kedacore https://kedacore.github.io/charts
      
    • System Response
      root@sdn-k8s-b2-1:~# helm repo add kedacore https://kedacore.github.io/charts
      "kedacore" has been added to your repositories
      
    1. 更新 helm repository
      helm repo update
      
    • System Response
      root@sdn-k8s-b2-1:~# helm repo update
      Hang tight while we grab the latest from your chart repositories...
      ...Successfully got an update from the "kedacore" chart repository
      ...Successfully got an update from the "istio.io" chart repository
      Update Complete. ⎈ Happy Helming!⎈ 
      
    1. 添加 kubernetes namespace
      kubectl create namespace keda
      
    • System Response
      root@sdn-k8s-b2-1:~# kubectl create namespace keda
      namespace/keda created
      
    1. helm 安裝 keda
      helm install keda kedacore/keda --namespace keda
      
    • System Response
      root@sdn-k8s-b2-1:~# helm install keda kedacore/keda --namespace keda
      manifest_sorter.go:192: info: skipping unknown hook: "crd-install"
      manifest_sorter.go:192: info: skipping unknown hook: "crd-install"
      NAME: keda
      LAST DEPLOYED: Thu Jun  4 06:24:14 2020
      NAMESPACE: keda
      STATUS: deployed
      REVISION: 1
      TEST SUITE: None
      
    1. 檢查部署狀態
      kubectl get deploy -n keda
      
    • System Response
      root@sdn-k8s-b2-1:~# kubectl get deploy -n keda
      NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
      keda-operator                     1/1     1            1           8m55s
      keda-operator-metrics-apiserver   1/1     1            1           8m55s
      
  • KEDA 移除

    helm uninstall -n keda keda
    kubectl delete -f https://raw.githubusercontent.com/kedacore/keda/master/deploy/crds/keda.k8s.io_scaledobjects_crd.yaml
    kubectl delete -f https://raw.githubusercontent.com/kedacore/keda/master/deploy/crds/keda.k8s.io_triggerauthentications_crd.yaml
    

kubectl 部署方案

  • KEDA 安裝

    • Github Realease Package

      kubectl apply -f ./crds
      kubectl apply -f ./
      
    • Github Repository Clone

      git clone https://github.com/kedacore/keda && cd keda
      kubectl apply -f deploy/crds/keda.k8s.io_scaledobjects_crd.yaml
      kubectl apply -f deploy/crds/keda.k8s.io_triggerauthentications_crd.yaml
      kubectl apply -f deploy/
      
  • KEDA 移除

    kubectl delete -f deploy/crds/keda.k8s.io_scaledobjects_crd.yaml
    kubectl delete -f deploy/crds/keda.k8s.io_triggerauthentications_crd.yaml
    kubectl delete -f deploy/
    

RabbitMQ 通訊訊息觸發測試

  1. Helm 3 的 stable 準備
    helm repo add stable https://kubernetes-charts.storage.googleapis.com
    
  • System Response
    root@sdn-k8s-b2-1:~/sample-go-rabbitmq# helm repo add stable  https://kubernetes-charts.storage.googleapis.com
    "stable" has been added to your repositories
    
  1. RabbitMQ 安裝
    helm install rabbitmq --set rabbitmq.username=user,rabbitmq.password=PASSWORD stable/rabbitmq
    
  • System Response
    root@sdn-k8s-b2-1:~/sample-go-rabbitmq# helm install rabbitmq --set rabbitmq.username=user,rabbitmq.password=PASSWORD stable/rabbitmq
    WARNING: This chart is deprecated
    NAME: rabbitmq
    LAST DEPLOYED: Thu Jun  4 06:54:28 2020
    NAMESPACE: default
    STATUS: deployed
    REVISION: 1
    TEST SUITE: None
    

2.1 RabbitMQ bug 修復

部署完之後,會出現 PV 不存在掛不起來的問題,這邊就補一個給他。

  • 生一個 PV 給他

    rabbitmq-pv.yaml
    ----
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: rabbitmq-pv
    spec:
      capacity:
        storage: 8Gi
      accessModes:
      - ReadWriteOnce
      hostPath:
        path: /tmp
    
  • 部署剛剛配置的 PV

    kubectl apply -f rabbitmq-pv.yaml
    

  1. 開擼 RabbitMQ 在 KEDA 上的拓展機制
  • RabbitMQ 的 Deployment

這邊就是會被拓展的 Deployment 的基底

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rabbitmq-consumer
  namespace: default
  labels:
    app: rabbitmq-consumer
spec:
  selector:
    matchLabels:
      app: rabbitmq-consumer
  template:
    metadata:
      labels:
        app: rabbitmq-consumer
    spec:
      containers:
        - name: rabbitmq-consumer
          image: jeffhollan/rabbitmq-client:dev
          imagePullPolicy: Always
          command:
            - receive
          args:
            - "amqp://user:PASSWORD@rabbitmq.default.svc.cluster.local:5672"
  • Scale Object 拓展規則配置

這邊會對應你的 deployment 名稱,還有一些關於他的拓展行為。
spec.scaleTargetRef.deploymentName: 對應你的拓展 Deployment 名稱
spec.minReplicaCount: 最小保留 Pod 數
spec.maxReplicaCount: 最大拓展 Pod 數
spec.pollingInterval: 輪詢時間 (每 N 秒向事件的監聽 Server 端確認,駐列中等待消化的訊息數量)
spec.cooldownPeriod: 冷卻時間(過了 N 秒後無事件就開始縮小 Pod 數量)
spec.triggers.type: KEDA 提取的觸發種類
spec.triggers.metadata.queueName: 訂閱的 RabbitMQ 通道名稱
spec.triggers.metadata.queueLength: 每 N 筆訊息開一個 Pod

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: rabbitmq-consumer
  namespace: default
  labels:
    deploymentName: rabbitmq-consumer
spec:
  scaleTargetRef:
    deploymentName: rabbitmq-consumer
  pollingInterval: 5 # Optional. Default: 30 seconds
  cooldownPeriod: 30 # Optional. Default: 300 seconds
  maxReplicaCount: 30 # Optional. Default: 100
  triggers:
    - type: rabbitmq
      metadata:
        queueName: hello
        queueLength: "5"
      authenticationRef:
        name: rabbitmq-consumer-trigger
  • 觸發種類參考

https://keda.sh/docs/2.0/scalers/artemis/

  • TriggerAuthentication 觸發機制撈資料的相關權限配置

這邊就是 KEDA 會去找這邊的資料當作提取你的 RabbitMQ 的使用者權限

apiVersion: keda.k8s.io/v1alpha1
kind: TriggerAuthentication
metadata:
  name: rabbitmq-consumer-trigger
  namespace: default
spec:
  secretTargetRef:
    - parameter: host
      name: rabbitmq-consumer-secret
      key: RabbitMqHost
  1. RabbitMQ 訊息接收者部署
    kubectl create -f deploy/deploy-consumer.yaml
    
    • System Response
    root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl apply -f deploy/deploy-consumer.yaml
    secret/rabbitmq-consumer created
    deployment.apps/rabbitmq-consumer created
    scaledobject.keda.k8s.io/rabbitmq-consumer created
    
    • kubectl Deploy 狀態
    root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl get deploy
    NAME                READY   UP-TO-DATE   AVAILABLE   AGE
    rabbitmq-consumer   1/1     1            1           5s
    
  2. RabbitMQ 訊息發送者部署
    kubectl apply -f deploy/deploy-publisher-job.yaml
    
    • System Response
    root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl apply -f deploy/deploy-publisher-job.yaml
    job.batch/rabbitmq-publish created
    
  3. 觀看 RabbitMQ 接收訊息時 Deployment 拓展 Pod
    kubectl get deploy -w
    
    • System Response
      root@sdn-k8s-b2-1:~/sample-go-rabbitmq# kubectl get deploy -w
      NAME                READY   UP-TO-DATE   AVAILABLE   AGE
      rabbitmq-consumer   0/1     1            0           16s
      rabbitmq-consumer   1/1     1            1           26s
      rabbitmq-consumer   0/1     1            0           27s
      rabbitmq-consumer   0/0     1            0           29s
      rabbitmq-consumer   0/0     1            0           29s
      rabbitmq-consumer   0/0     1            0           29s
      rabbitmq-consumer   0/0     0            0           29s
      rabbitmq-consumer   0/1     0            0           34s
      rabbitmq-consumer   0/1     0            0           34s
      rabbitmq-consumer   0/1     0            0           34s
      rabbitmq-consumer   0/1     1            0           34s
      rabbitmq-consumer   1/1     1            1           40s
      rabbitmq-consumer   1/4     1            1           45s
      rabbitmq-consumer   1/4     1            1           45s
      rabbitmq-consumer   1/4     1            1           45s
      rabbitmq-consumer   1/4     4            1           45s
      rabbitmq-consumer   2/4     4            2           50s
      rabbitmq-consumer   3/4     4            3           52s
      rabbitmq-consumer   4/4     4            4           55s
      rabbitmq-consumer   4/8     4            4           60s
      rabbitmq-consumer   4/8     4            4           60s
      rabbitmq-consumer   4/8     4            4           60s
      rabbitmq-consumer   4/8     8            4           60s
      rabbitmq-consumer   5/8     8            5           65s
      rabbitmq-consumer   6/8     8            6           68s
      rabbitmq-consumer   7/8     8            7           70s
      rabbitmq-consumer   8/8     8            8           73s
      rabbitmq-consumer   8/16    8            8           75s
      rabbitmq-consumer   8/16    8            8           75s
      rabbitmq-consumer   8/16    8            8           75s
      rabbitmq-consumer   8/16    16           8           75s
      rabbitmq-consumer   9/16    16           9           80s
      rabbitmq-consumer   10/16   16           10          82s
      rabbitmq-consumer   11/16   16           11          84s
      rabbitmq-consumer   12/16   16           12          87s
      rabbitmq-consumer   13/16   16           13          89s
      rabbitmq-consumer   13/30   16           13          90s
      rabbitmq-consumer   13/30   16           13          90s
      rabbitmq-consumer   13/30   16           13          90s
      rabbitmq-consumer   13/30   30           13          90s
      rabbitmq-consumer   14/30   30           14          94s
      rabbitmq-consumer   15/30   30           15          95s
      rabbitmq-consumer   16/30   30           16          97s
      rabbitmq-consumer   17/30   30           17          99s
      rabbitmq-consumer   18/30   30           18          102s
      rabbitmq-consumer   19/30   30           19          104s
      rabbitmq-consumer   20/30   30           20          106s
      rabbitmq-consumer   21/30   30           21          110s
      rabbitmq-consumer   22/30   30           22          112s
      rabbitmq-consumer   23/30   30           23          114s
      rabbitmq-consumer   24/30   30           24          117s
      rabbitmq-consumer   25/30   30           25          119s
      rabbitmq-consumer   25/0    30           25          2m
      rabbitmq-consumer   25/0    30           25          2m
      rabbitmq-consumer   25/0    30           25          2m
      rabbitmq-consumer   0/0     0            0           2m
      

上一篇
Day 20 GCP 公有雲_雲端事件消息傳遞服務實戰 - Pub/Sub 組建測試之路
下一篇
Day 22 基於事件消息傳遞服務拓展實戰 - KEDA 組建測試之路 GCP Pub/Sub 篇
系列文
基於付費公有雲與開源機房自建私有雲之雲端應用服務測試兼叢集與機房託管服務實戰之勇者崎嶇波折且劍還掉在路上的試煉之路30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言